<template>
<!--  @ok="handleSubmit"-->

  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="100%"
    @ok="handleSubmit"
    @cancel="closeFn"
  >


    <div style="overflow: hidden; width: 100%;" :style="{height: (contentHeight*0.85)+'px'}">
<!--      :data="dataArr"-->
      <div style="margin-left: 10px" v-if="selectOrderNo.length>0">已排除订单标识号:{{selectOrderNo}}</div>
      <VxeBasicTable ref="tableRef" v-bind="gridOptions" >
        <template #action="{ row }">
          <TableAction outside :actions="getActions(row)" />
<!--          <TableAction outside :actions="[-->
<!--          {-->
<!--              label:'查看',-->
<!--              onClick: addDknClosePlanFn.bind(null, row),-->
<!--          },]-->
<!--          "/>-->
        </template>
      </VxeBasicTable>
    </div>

  </BasicDrawer>
</template>
<script lang="ts" setup>

/**
 *  选择 零售订单
 */

import {ref, computed, unref, reactive} from 'vue';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';

  import {BasicTable, useTable, TableAction, FormSchema} from '@/components/Table';
 import { queryDknTableList,addDknClosePlan,removeDknClosePlan } from '@/api/heng/dkn';
import {
  vxeTableColumns,
} from './role.data';

import {
  BasicTableProps,
  VxeBasicTable,
  VxeGridInstance,
  VxeGridPropTypes
} from '@/components/VxeTable';


  const props = defineProps({
    mode: { type: String,default:"multiple" },
    isAdd: {type:Boolean,default:false},
    selectOrderNo:{type:Array}

  });
import {

} from "@/api/sys/msUser";
  import { Form,FormItem,Switch,Input } from 'ant-design-vue';

  import {useMessage} from "@/hooks/web/useMessage";
  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const { createMessage } = useMessage();
  const formRef=ref();
  // const checked=ref(false);
  const dataArr=ref([]);

const title=ref("")

const contentHeight = ref(window.innerHeight);


const tableRef = ref<VxeGridInstance>();
const selectOrderNo=ref([]);//已排除

const gridOptions = reactive<BasicTableProps>({
  id: 'VxeTable',
  keepSource: true,
  editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
  showOverflow: true,
  size:"mini",
  height: 'auto',
  columns:vxeTableColumns,
  toolbarConfig: {
    // buttons: [],
    enabled:true//禁用右侧打印等工具栏
  },
  exportConfig:{},
  sortConfig:{
    field: 'requireDate',
    order: 'asc'
  },
  radioConfig: {
    highlight: true
  },
  formConfig: {
    data: {
      // biaoShiHao: '',
    },
    items: [
      { field: 'biaoShiHao', title: '订单标识号', itemRender: { name: 'VxeInput' } },
      { field: 'izClose', title: '是否关闭',
        itemRender: {
          defaultValue:"0",
          name: 'VxeSelect',
          props: {
            clearable:true,
          },
          options:[
            {label:"已关闭",value:"1"},{label:"未关闭",value:"0"}]
        },
      },
      { field: 'izWeiPlan',
        title: '是否安排',
        itemRender: {
          defaultValue:1,
          name: 'VxeSelect',
          props: {
            clearable:true,
          },
          options:[
            {label:"未安排",value:1},{label:"已安排",value:2}]
        },
      },
      {
        itemRender: {
          name: 'VxeButtonGroup',
          options: [
            { type: 'submit', content: '搜索', status: 'primary' },
            { type: 'reset', content: '重置' }
          ]
        }
      }
    ]
  },
  rowConfig: {
    // labelField: 'billNo',
    // trigger: 'row'
  },
  pagerConfig: {
    pageSize: 50,
  },
  proxyConfig: {
    // enabled:false
    props:{
      result:"data.list",
      list:"data.list",
      total:"data.totalResult",
      message:"message"
    },
    ajax: {
      query: async ({ page, form,sorts }) => {
        let json={
          // izWeiPlan:1
        };
        if(selectOrderNo.value){
          json.biaoShiHaoNoIns=selectOrderNo.value.join(",");
        }
        return queryDknTableList({
          page: page.currentPage,
          pageSize: page.pageSize,
          ...form,
          ...json
        });
      },

    },
  },
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    try {
      gridOptions.formConfig.items[0].itemRender.defaultValue=data.data;
    }catch(err) {
      console.log(err);
    }
    selectOrderNo.value=data.selectOrderNo||[];
    title.value= data.title;
  // console.log("--data.isAdd:",data.isAdd)
    //orderNo用于判断是否切换为输入框模式 dataArr
    // let arr=
    //   [
    //   {biaoShiHao: 'W2505015001',mtid:'01010013',model:'W200S V2',guige:'黑色',planDate:'2025-08-08',requireDate:'2025-08-18',orderNum:3600,thisNum:3600},
    //   {biaoShiHao: 'W2507026001',mtid:'0101001',model:'W100',guige:'蓝色',planDate:'2025-08-08',requireDate:'2025-08-11',orderNum:2400,thisNum:2400},
    //   ];
    //
    // dataArr.value = arr;
  tableRef.value?.commitProxy("reload")
  });


  const getTitle = computed(() => (unref(title.value) ? title.value : '选择排产计划'));//isAdd


  async function handleSubmit() {

    if(title.value&&title.value=="查看排产计划"){
      closeFn();
      return ;
    }


    let selectRows = tableRef.value.getRadioRecord();


    if(!selectRows){
      createMessage.error("请先勾选排产计划!");
      return false;
    }
    emit('success',selectRows);
    closeFn();


  }
  function closeFn(){
    // tableRef.value.clearRadioRow();
    tableRef.value.commitProxy('reload')
    closeDrawer()
  }

function getActions(record) {
  let arr=[

  ];
  if(!record.closeId){
    arr.push(
      {
        label: '关闭',
        color: 'error',
        // tooltip: '关闭此计划',
        popConfirm: {
          title: '是否关闭此计划',
          placement: 'left',
          confirm: addDknClosePlanFn.bind(null, record),
        },
      }
    )
  }else{
    arr.push( {
      // icon: 'ant-design:delete-outlined',
      color: 'success',
      label:"启用",
      popConfirm: {
        title: '是否启用计划',
        placement: 'left',
        confirm: removeDknClosePlanFn.bind(null, record),
      },
    })
  }
  return arr;
}

function addDknClosePlanFn(record){
  addDknClosePlan({biaoShiHao: record.biaoShiHao}).then(res=>{
    if(res.code==200){
      createMessage.success(res.message);
      tableRef.value.commitProxy('reload');
    }else{
      createMessage.error(res.message);
    }
  });
}

function removeDknClosePlanFn(record){
  removeDknClosePlan({biaoShiHao: record.biaoShiHao}).then(res=>{
    if(res.code==200){
      createMessage.success(res.message);
      tableRef.value.commitProxy('reload');
    }else{
      createMessage.error(res.message);
    }
  });
}

</script>
<style lang="less">
.lqforder-h-group{
  display: flex;
  padding-left: 6.5%;
  justify-items: center;
  margin-bottom: 18px;
  .g-lable{
    margin-right: 8px;
    align-items: center;
    display: flex;
  }
}
.my-table-list {
  border: 1px solid #e8eaec;
  table {
    width: 100%;
    text-align: center;
    border-spacing: 0;
  }
  tr {
    //height: 30px;
    &:hover {
      background-color: #f5f7fa;
    }
  }
  th {
    //height: 40px;
    background-color: #f8f8f9;
    position: sticky;
    top: 0;
  }
  th,
  td {
    border-bottom: 1px solid #e8eaec;
    border-right: 1px solid #e8eaec;
  }
}

</style>
